//
//  DeliveredJobListTableViewCell.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/24.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "DeliveredJobListTableViewCell.h"

@implementation DeliveredJobListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
	self.rightBtn1.layer.cornerRadius = 12;
	self.rightBtn2.layer.cornerRadius = 12;
	self.rightBtn1.backgroundColor = UIMainColorRGB;
	self.rightBtn2.backgroundColor = UIMainColorRGB;
}

- (void)setInfo:(NSDictionary *)info
{
	if (_info != info) {
		_info = info;
		self.companyName.text = info[@"company_title"];
		self.jobTitle.text = info[@"category_title"];
		self.salary.text = info[@"salary"];
		[self.Logo sd_setImageWithURL:[NSURL URLWithString:info[@"logo"]] placeholderImage:IMAGE(@"placeHolder")];
		self.companySize.text = info[@"company_size"];
		self.industry.text = info[@"industry_field"];
		self.workExp.text = info[@"work_life"];
		self.education.text = info[@"education"];
		self.district.text = info[@"district"];
		NSString *stateStr;
		if (self.type == 0) {
			switch ([info[@"status"] integerValue]) {
				case 0:
					stateStr = @"待查看";
					break;
				case 1:
					stateStr = @"已查看";
					break;
				case 2:
					stateStr = @"已邀约";
					break;
				case 3:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		else {
			switch ([info[@"status"] integerValue]) {
				case 0:
					stateStr = @"待查看";
					break;
				case 1:
					stateStr = @"已查看";
					break;
				case 2:
					stateStr = @"已同意";
					break;
				case 3:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		
		self.state.text = stateStr;
		if (self.type == 1 && [info[@"status"] integerValue] != 3 && [info[@"status"] integerValue] != 2) {
			self.rightBtn1.hidden = NO;
			self.rightBtn2.hidden = NO;
			[self.rightBtn2 addTarget:self action:@selector(rejectInvitation:) forControlEvents:UIControlEventTouchUpInside];
			[self.rightBtn1 addTarget:self action:@selector(agreeInvitation:) forControlEvents:UIControlEventTouchUpInside];
		}
		else
		{
			self.rightBtn1.hidden = YES;
			self.rightBtn2.hidden = YES;
		}
	}
}

-(void)setModel:(InterviewManageModel *)model
{
	if (_model != model) {
		_model = model;
		self.companyName.text = model.company_title;
		self.jobTitle.text = model.category_title;
		self.salary.text = model.salary;
		[self.Logo sd_setImageWithURL:[NSURL URLWithString:model.company_logo] placeholderImage:IMAGE(@"placeHolder")];
		self.companySize.text = model.company_size;
		self.industry.text = model.industry_field;
		self.workExp.text = model.work_life;
		self.education.text = model.education;
		self.district.text = model.district;
		NSString *stateStr;
		self.rightBtn1.hidden = YES;
		self.rightBtn2.hidden = YES;
		self.timeL.hidden = NO;
		self.interviewTime.hidden = NO;
		self.interviewTime.text = [self changeTimeStamptoString:model.interview_time];
		self.remarkL.hidden = YES;
		self.remark.hidden = YES;
		self.remark.text = @"";//由于要自适应高度，当有些不需要的页面直接给空
		if (self.type == 2) {
			switch ([model.result integerValue]) {
				case 0:
					stateStr = @"待回复";
					break;
				case 2:
					stateStr = @"通过面试";
					self.rightBtn1.hidden = NO;
					[self.rightBtn1 setTitle:@"同意" forState:UIControlStateNormal];
					self.rightBtn2.hidden = NO;
					[self.rightBtn2 setTitle:@"拒绝" forState:UIControlStateNormal];
					[self.rightBtn1 addTarget:self action:@selector(agreeEmploy:) forControlEvents:UIControlEventTouchUpInside];
					[self.rightBtn2 addTarget:self action:@selector(disagreeEmploy:) forControlEvents:UIControlEventTouchUpInside];
					break;
				case -1:
					stateStr = @"已拒绝";
					break;
				case 1:
					stateStr = @"已同意";
					break;
				default:
					break;
			}
		}
		else if(self.type == 3)
		{
			switch ([model.result integerValue]) {
				case 1:
					stateStr = @"已同意";
					break;
				case -1:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		else
		{
			self.seperateLine.hidden = NO;
			switch ([model.is_confirmed integerValue]) {
				case 0:
					stateStr = @"未回复";
					self.rightBtn1.hidden = NO;
					[self.rightBtn1 setTitle:@"同意" forState:UIControlStateNormal];
					self.rightBtn2.hidden = NO;
					[self.rightBtn2 setTitle:@"协调时间" forState:UIControlStateNormal];
					[self.rightBtn1 addTarget:self action:@selector(agreeDate:) forControlEvents:UIControlEventTouchUpInside];
					[self.rightBtn2 addTarget:self action:@selector(discussDate:) forControlEvents:UIControlEventTouchUpInside];
					self.remarkL.hidden = NO;
					self.remark.hidden = NO;
					self.remark.text = model.remark;
					break;
				case 1:
					stateStr = @"已确认";
					self.remarkL.hidden = NO;
					self.remark.hidden = NO;
					self.remark.text = model.remark;
					break;
				case 2:
					stateStr = @"等待企业选择时间";
					self.remarkL.hidden = NO;
					self.remark.hidden = NO;
					self.remark.text = model.remark;
					break;
				default:
					break;
			}
		}
		if (self.remark.text.length == 0) {
			self.remark.text = @"暂无备注";
		}
		else {
			self.remark.text = [model.remark stringByAppendingString:@"\n"];
		}
		self.state.text = stateStr;
	}
}
- (void)agreeDate:(UIButton *)sender
{
	[self.delegate2 agreeDate:self.model.interview_id];
}

- (void)discussDate:(UIButton *)sender
{
	[self.delegate2 dicussDate:self.model.interview_id];
}

- (void)agreeEmploy:(UIButton *)sender
{
	[self.delegate2 agreeEmploy:YES interviewId:self.model.interview_id];
}

- (void)disagreeEmploy:(UIButton *)sender
{
	[self.delegate2 agreeEmploy:NO interviewId:self.model.interview_id];
}

- (NSString *)changeTimeStamptoString:(NSString *)timeStampString {
	
	// iOS 生成的时间戳是10位
	NSTimeInterval interval = [timeStampString doubleValue];
	
	if (interval == 0) {
		return @"";
	}
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSString *dateString = [formatter stringFromDate: date];
	NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
	
	return dateString;
}

- (void)agreeInvitation:(UIButton *)sender
{
	[self.delegate1 agreeInvitation:self.info[@"delivery_id"]];
}

- (void)rejectInvitation:(UIButton *)sender
{
	[self.delegate1 rejectInvitation:self.info[@"delivery_id"]];
}

- (void)setFrame:(CGRect)frame
{
	frame.origin.y += 10;
	
	frame.size.height -= 10;
	
	[super setFrame:frame];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
